package com.cjack.frenchexcel.controller;

import com.cjack.frenchexcel.config.AjaxResult;
import com.cjack.frenchexcel.config.CommonConfig;
import com.cjack.frenchexcel.config.ErrorCodeDefine;
import com.cjack.frenchexcel.config.LayPage;
import com.cjack.frenchexcel.exception.CommonException;
import com.cjack.frenchexcel.model.QontoModel;
import com.cjack.frenchexcel.service.QontoService;
import com.cjack.frenchexcel.table.QontoTable;
import com.cjack.frenchexcel.util.DateFormatUtil;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Date;

/**
 * 以前是qonto银行，现在是Banque CE银行，但是依然沿用以前的数据库和页面
 */
@RestController
@RequestMapping("/qonto")
public class QontoEndpoint extends BaseEndpoint{

    @Autowired
    QontoService service;

    /**
     * 列表
     * @return
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    public AjaxResult list( LayPage layPage, QontoModel condition) {
        try{
            Page< QontoTable> resultPage = service.list( layPage, condition);
            layPage.setData( resultPage.getContent());
            layPage.setCount( Long.valueOf( resultPage.getTotalElements()).intValue());
            return AjaxResult.SUCCESS( layPage);
        }catch ( Exception e) {
            e.printStackTrace();
            return AjaxResult.ERROR();
        }
    }

    /**
     * 主动同步数据，同步数据成功了之后需要重新加载表格
     * 同步今天0点到现在的数据
     * @return
     */
    @RequestMapping(value = "/syncData", method = RequestMethod.POST)
    @ResponseBody
    public AjaxResult syncData() {
        try{
            String startTime = DateFormatUtil.format( DateFormatUtil.getLastDayZero( 0), DateFormatUtil.DATE_RULE_6);
            String endTime = DateFormatUtil.format( new Date(), DateFormatUtil.DATE_RULE_6);;
            service.getTransactionsFromQonto( startTime, endTime);
            return AjaxResult.SUCCESS();
        }catch ( Exception e) {
            e.printStackTrace();
            return AjaxResult.ERROR();
        }
    }

    /**
     * 从excel里面导入数据，分成两步来弄
     * 1、先调用upload接口上传excel文件到服务器
     * 2、用第一步上传返回的excel地址调用此接口
     * 从excel导入数据
     * @param filePath  excel的浏览器端访问路径
     * @return
     */
    @RequestMapping(value = "/importFromExcel", method = RequestMethod.POST)
    @ResponseBody
    public AjaxResult importFromExcel(HttpSession session, @RequestParam("filePath") String filePath) {

        try{
            if( StringUtils.isEmpty( filePath)){
                throw new CommonException(ErrorCodeDefine.INTERFACE_COMMON_ERROR, "filePath不能为空");
            }
            Object adminUserId = session.getAttribute( CommonConfig.SESSION_NAME);
            //注意pageDate里面的页数是从0开始计算的
            JSONObject json = service.importFromExcel( filePath);
            //返回重复的数据
            return AjaxResult.SUCCESS( json);
        }catch ( CommonException e) {
            e.printStackTrace();
            return AjaxResult.ERROR( e.getCode(), e.getMessage());
        }catch ( Exception e) {
            e.printStackTrace();
            return AjaxResult.ERROR();
        }
    }

    /**
     * 获取对账数据
     * @return
     */
    @RequestMapping(value = "/getAccountTotal", method = RequestMethod.GET)
    @ResponseBody
    public AjaxResult getAccountTotal() {
        try{
            Double total = service.getAccountTotal();
            return AjaxResult.SUCCESS( total);
        }catch ( Exception e) {
            e.printStackTrace();
            return AjaxResult.ERROR();
        }
    }

    @RequestMapping(value = "/uploadInvoice", method = RequestMethod.POST)
    @ResponseBody
    public AjaxResult uploadInvoice(HttpSession session, QontoModel model) {
        try{
            Object loginUserId = session.getAttribute( CommonConfig.SESSION_NAME);
            model.setInvoiceUploadUserId( (Long)loginUserId);
            model.setUpdateTime( new Date());
            service.add( model);
            return AjaxResult.SUCCESS();
        }catch ( Exception e) {
            e.printStackTrace();
            return AjaxResult.ERROR();
        }
    }

    /**
     * 审核
     * @return
     */
    @RequestMapping(value = "/reviewInvoice", method = RequestMethod.POST)
    @ResponseBody
    public AjaxResult reviewInvoice( QontoModel model) {
        try{
            model.setUpdateTime( new Date());
            model.setInvoiceReviewTime( new Date());
            service.add( model);
            return AjaxResult.SUCCESS( "审核成功");
        }catch ( Exception e) {
            e.printStackTrace();
            return AjaxResult.ERROR();
        }
    }

    /**
     * 根据id获取下一条待审核的数据
     * @return
     */
    @RequestMapping(value = "/getLastById", method = RequestMethod.POST)
    @ResponseBody
    public AjaxResult getLastById( Long id) {
        try{
            return AjaxResult.SUCCESS( service.getLastById( id));
        }catch ( Exception e) {
            e.printStackTrace();
            return AjaxResult.ERROR();
        }
    }
}
